/* 
 *  Created By: Mike Stevens
 *  Project URL: http://www.codeplex.com/sitesecuritymgmt
 *  License: GNU GPL v2
 *  Copyright (C) 2008 Mike Stevens
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace SiteSecurityManagement.Data
{
    [Serializable]
    class DataTableWrapper
    {
        #region Members
        private DataTable m_dataTable;
        #endregion

        #region Constructor/Destructor
        public DataTableWrapper()
        {
            m_dataTable = new DataTable();
        }

        public DataTableWrapper(string tableName)
        {
            m_dataTable = new DataTable(tableName);
        }

        public DataTableWrapper(DataTable dataTable)
        {
            m_dataTable = dataTable;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the items.
        /// </summary>
        /// <value>A list of the data items as an object array.</value>
        public List<object[]> GetItems
        {
            get 
            {
                List<object[]> list = new List<object[]>();
                foreach (DataRow  row in m_dataTable.Rows)
                {
                    list.Add(row.ItemArray);
                }
                return list;
            }
        }

        /// <summary>
        /// Gets a bindable data source.
        /// </summary>
        /// <value>The bindable data source.</value>
        public DataView BindableDataSource
        {
            get { return m_dataTable.DefaultView; }
        }
	
        #endregion

        #region Methods
        /// <summary>
        /// Deletes the row.
        /// </summary>
        /// <param name="rowIndex">Index of the row.</param>
        public void DeleteRow(int rowIndex)
        {
            m_dataTable.Rows[rowIndex].Delete();
        }

        /// <summary>
        /// Gets the row count.
        /// </summary>
        /// <returns></returns>
        public int GetRowCount()
        {
            return m_dataTable.Rows.Count;
        }

        /// <summary>
        /// Adds the data row.
        /// </summary>
        /// <param name="rowValues">The row values.</param>
        public void AddDataRow(Dictionary<string, string> rowValues)
        {
            DataRow row = m_dataTable.NewRow();
            foreach (KeyValuePair<string, string> keyValue in rowValues)
            {
                row[keyValue.Key] = keyValue.Value;
            }

            AddDataRow(row);
        }

        /// <summary>
        /// Adds the data row.
        /// </summary>
        /// <param name="dataRow">The data row.</param>
        public void AddDataRow(DataRow dataRow)
        {
            m_dataTable.Rows.Add(dataRow);
        }

        /// <summary>
        /// Adds the data column.
        /// </summary>
        /// <param name="columnName">Name of the column.</param>
        /// <param name="columnType">Type of the column.</param>
        /// <param name="autoIncrement">if set to <c>true</c> [auto increment].</param>
        public void AddDataColumn(string columnName, string columnType,bool autoIncrement)
        {
            DataColumn dataColumn = new DataColumn(columnName,Type.GetType(columnType));
            dataColumn.AutoIncrement = autoIncrement;
            
            AddDataColumn(dataColumn);
        }

        /// <summary>
        /// Adds the data column.
        /// </summary>
        /// <param name="dataColumn">The data column.</param>
        public void AddDataColumn(DataColumn dataColumn)
        {
            if (false == m_dataTable.Columns.Contains(dataColumn.ColumnName))
            {
                m_dataTable.Columns.Add(dataColumn);
            }
        }
        #endregion
    }
}
